package com.vip.jwt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.vip.jwt.entity.AppVersion;
import com.vip.jwt.mapper.AppVersionMapper;
import com.vip.jwt.service.AppVersionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author vip
 * @since 2021-07-24
 */
@Service
public class AppVersionServiceImpl extends ServiceImpl<AppVersionMapper, AppVersion> implements AppVersionService {

    @Resource
    private AppVersionMapper appVersionMapper;

    @Override
    public AppVersion checkUpdate(AppVersion appVersion) {
        boolean setupPage = appVersion.getSetupPage();
        QueryWrapper<AppVersion> appVersionQueryWrapper = new QueryWrapper<>();
        appVersionQueryWrapper.eq("type",appVersion.getType());
        appVersionQueryWrapper.orderByDesc("create_time");
        List<AppVersion> appVersions = appVersionMapper.selectList(appVersionQueryWrapper);
        if(appVersions!=null&&appVersions.size()>0){
            AppVersion findAppVersion = appVersions.get(0);
            //如果最新的版本号大于客户端传来的版本号，则更新
            if(findAppVersion.getVersionCode()>appVersion.getVersionCode()){
                //如果用户在设置页面手动检查的更新
                if(setupPage){
                    //则将更新类型设置为弹窗更新
                    findAppVersion.setUpdateType("solicit");
                }
                return findAppVersion;
            }
        }
        return null;
    }

}
